/*****************************************************************************
 * (C) Copyright 2020 Atech-Automotive
 * FILE NAME:    app_sci1.c
 * DESCRIPTION:  
 * DATE BEGUN:   
 * BY:           
 * PRODUCT NAME: 
 * APPLICATION:  
 * TARGET H/W:   
 * DOC REF:
 *****************************************************************************
 */
/* #include */
#include <string.h>
#include "app_sci1.h"
#include "drv_SCI.h"
#include "app_sci1_Itf.h"
#include "msg_sci1List.h"

static BOOL b_sci1EnableTx = TRUE;
static BOOL b_sci1EnableRx = TRUE;

//static UINT8 u8_sci1TxErrCount = 0;

//static BOOL b_sci1RxKeepFlg = FALSE;/*if msg be received in 10ms, it will be set*/
//static UINT8 u8_sci1RxKeepTimer = 0;

/* static function prototypes */
static void app_sci1RxMsgs(void);
static void app_sci1UpdateTxMsgs(void);
static void app_sci1CheckTimeout(void);
static void app_sci1CheckRxCTimeOutError(SCI_RX_MSG_S* p_s_sci1Msg);
static void app_sci1CheckRxLongCTimeOutError(SCI_RX_LONG_MSG_S* p_s_sci1Msg);
static void app_sci1CheckRxEventTimeOutError(UINT8 id,UINT8 sub_id);
static void app_sci1CheckRxLongEventTimeOutError(UINT8 id,UINT8 sub_id);
static void app_sci1WriteWFeedbackFlag(UINT8 id,UINT8 sub_id,BOOL set);



static void app_sci1CycTxMsgs(void);
static void app_sci1MoniterErr(void);



/******************************************************************************/
/**
 * Function Name: Comm_sci1_vHandler10ms
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				        					yong.ding
 ******************************************************************************/

void Comm_sci1_vHandler10ms(void)
{
    app_sci1RxMsgs();
    app_sci1CheckTimeout();
    app_sci1UpdateTxMsgs();
    app_sci1CycTxMsgs(); //MING TIAN XU YAO JIA SHANG LONG TXMSG
    app_sci1MoniterErr();
}

#define SCI_TX_MSG
/******************************************************************************/
/**
 * Function Name: app_sci1UpdateTxMsgs
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				        					yong.ding
 ******************************************************************************/

static void app_sci1UpdateTxMsgs(void)
{
    app_sci1ItfUpdataTxMsgs();   
}



/******************************************************************************/
/**
 * Function Name: app_sci1CycTxMsgs
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				        					yong.ding
 ******************************************************************************/

static void app_sci1CycTxMsgs(void)
{
    SCI_TX_MSG_S **ptr;
	SCI_TX_LONG_MSG_S **ptr2;
    ptr = p_s_sci1CycTxTable;

/* ban transmit SCI message*/
    if (FALSE == b_sci1EnableTx)
    {
    	return;
    }
/*allow transmit SCI messages*/
    ptr = p_s_sci1CycTxTable;
    while( (*ptr)->Id != 0 )
    {
        if((*ptr)->Enable)
        {
            /*cycle send*/
            if((*ptr)->SendType == SCI_CYCLE)
            {
                if((*ptr)->CTimer < (*ptr)->CycleTime)
                {
                    (*ptr)->CTimer++;
                }
                else/*time end*/
                {
                    /*reset cycle timer*/
                    (*ptr)->CTimer = 0;
                    /*send msg*/
					app_sci1ItfSendFrame((*ptr)->Id,(*ptr)->Sub_Id_exist,(*ptr)->Sub_Id,(*ptr)->Length,(*ptr)->Data);
                }
            }
            /*event send*/
            else if((*ptr)->SendType == SCI_EVENT)
            {
                if((*ptr)->Trigger)
                {
                    /*send msg*/
                    if (app_sci1ItfSendFrame((*ptr)->Id,(*ptr)->Sub_Id_exist,(*ptr)->Sub_Id,(*ptr)->Length,(*ptr)->Data) == FALSE)
                    {

                    }
                    else
                    {
                        /* transmit success */
                        (*ptr)->Trigger = FALSE;
						app_sci1WriteWFeedbackFlag((*ptr)->Id,(*ptr)->Sub_Id,TRUE);//set Waite_Feedback_Flag
						if((*ptr)->NobackRetry_cn >= (*ptr)->NobackRetry_cn_max)
						{
							app_sci1WriteWFeedbackFlag((*ptr)->Id,(*ptr)->Sub_Id,FALSE);//clear set Waite_Feedback_Flag
							(*ptr)->NobackRetry_cn = 0;
							(*ptr)->NobackTimer = 0;
						}

                    } 
                }
            }
            /*cycle and event send*/
            else if((*ptr)->SendType == SCI_CE)
            {

            }
        }
        ptr++;
    }

    ptr2 = p_s_sci1LongCycTxTable;
    while( (*ptr2)->Id != 0 )
    {
        if((*ptr2)->Enable)
        {
            /*cycle send*/
            if((*ptr2)->SendType == SCI_CYCLE)
            {
                if((*ptr2)->CTimer < (*ptr2)->CycleTime)
                {
                    (*ptr2)->CTimer++;
                }
                else/*time end*/
                {
                    /*reset cycle timer*/
                    (*ptr2)->CTimer = 0;
                    /*send msg*/
					app_sci1ItfSendFrame((*ptr2)->Id,(*ptr2)->Sub_Id_exist,(*ptr2)->Sub_Id,(*ptr2)->Length,(*ptr2)->Data);
                }
            }
            /*event send*/
            else if((*ptr2)->SendType == SCI_EVENT)
            {
                if((*ptr2)->Trigger)
                {
                    /*send msg*/
                    if (app_sci1ItfSendFrame((*ptr2)->Id,(*ptr2)->Sub_Id_exist,(*ptr2)->Sub_Id,(*ptr2)->Length,(*ptr2)->Data) == FALSE)
                    {

                    }
                    else
                    {
                        /* transmit success */
                        (*ptr2)->Trigger = FALSE;
						app_sci1WriteWFeedbackFlag((*ptr2)->Id,(*ptr2)->Sub_Id,TRUE);//set Waite_Feedback_Flag
						if((*ptr2)->NobackRetry_cn >= (*ptr2)->NobackRetry_cn_max)
						{
							app_sci1WriteWFeedbackFlag((*ptr2)->Id,(*ptr2)->Sub_Id,FALSE);//clear set Waite_Feedback_Flag
							(*ptr2)->NobackRetry_cn = 0;
							(*ptr2)->NobackTimer = 0;
						}

                    } 
                }
            }
            /*cycle and event send*/
            else if((*ptr2)->SendType == SCI_CE)
            {

            }
        }
        ptr2++;
    }

}

#define SCI_RX_MSG
/******************************************************************************/
/**
 * Function Name: app_sci1RxMsgs
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				        					yong.ding
 ******************************************************************************/

static void app_sci1RxMsgs(void)
{
    UINT8 u8_id;
	UINT8 u8_sub_id;
    UINT8 u8_length;
    UINT8 p_u8_data[16];
    UINT8 u8_sci1RdCounter = SCI_BUFFER_SIZE;  
	BOOL state = FALSE;
    SCI_RX_MSG_S **ptr = NULL_PTR;     

    /* Update sci1 message*/
    while (u8_sci1RdCounter && (app_sci1ItfGetRxMessageCount() > 0))//if SCIRxbuffer have new message, we sci1 get the new message
    {    
        u8_sci1RdCounter--;     /* make sure we don't stay here forever */		        
		
        state = app_sci1ItfGetMessage(&u8_id,&u8_sub_id,&u8_length, p_u8_data);

        if(b_sci1EnableRx && (state==TRUE))
        {
			if(u8_length <= 8)
			{
				ptr = p_s_sci1RxTable;	
			}
			else
			{
				ptr = p_s_sci1LongRxTable;
			}
            while((*ptr)->Id != 0)
            {
                if((*ptr)->Id == u8_id && ((*ptr)->Sub_Id == u8_sub_id))
                {
//                    (*ptr)->Length = u8_length;
                    (void)memcpy((*ptr)->Data, p_u8_data, u8_length);
                    (*ptr)->Error = FALSE;
                    (*ptr)->CTimer = (*ptr)->CycleTimeOut;
                    (*ptr)->UpdataState = TRUE; 
					(*ptr)->Waite_Feedback_Flag = FALSE;
//                    app_sci1ItfRecMsg(u32_id,u8_length,p_u8_data);
                    break;
                }
                ptr++;
            }
        }
    }
}

/******************************************************************************/
/**
 * Function Name: app_sci1CheckTimeout
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				        					yong.ding
 ******************************************************************************/

static void app_sci1CheckTimeout(void)
{
    SCI_RX_MSG_S **ptr;        
	SCI_RX_LONG_MSG_S **lptr;
     /*
     * check timeout
     */
    ptr = p_s_sci1RxTable;
    while((*ptr)->Id != 0)
    {   
        if((*ptr)->SendType == SCI_CYCLE)
        {
            app_sci1CheckRxCTimeOutError(*ptr);
        }
        if((*ptr)->SendType == SCI_EVENT && ((*ptr)->Waite_Feedback_Flag == TRUE))
        {
            app_sci1CheckRxEventTimeOutError((*ptr)->Id,(*ptr)->Sub_Id);
        }
        ptr++;
    }
	lptr = p_s_sci1LongRxTable;
    while((*lptr)->Id != 0)
    {   
        if((*lptr)->SendType == SCI_CYCLE)
        {
            app_sci1CheckRxLongCTimeOutError(*lptr);
        }
		if((*lptr)->SendType == SCI_EVENT && ((*lptr)->Waite_Feedback_Flag == TRUE))
        {
            app_sci1CheckRxLongEventTimeOutError((*lptr)->Id,(*lptr)->Sub_Id);
        }
        lptr++;
    }
}
    

/******************************************************************************/
/**
 * Function Name: app_sci1CheckTimeout
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				        					yong.ding
 ******************************************************************************/

static void app_sci1CheckRxCTimeOutError(SCI_RX_MSG_S* p_s_sci1Msg)
{
    if(p_s_sci1Msg->CTimer)
    {
        p_s_sci1Msg->CTimer--;            
    }
    else
    {
        p_s_sci1Msg->Error = TRUE;
        //data recover to inital value
        (void)memcpy(p_s_sci1Msg->Data,
                     p_s_sci1Msg->InitData,
                     p_s_sci1Msg->Length);
    }
}

/******************************************************************************/
/**
 * Function Name: app_sci1CheckTimeOutError
 * Description: none
 *
 * Param:   none
 * Return:  none
 * _____________________________________________________________________________
 *     Date             Changes Description                Author
 *  09/13/2020                draft                     yong.ding
 ******************************************************************************/
static void app_sci1CheckRxLongCTimeOutError(SCI_RX_LONG_MSG_S* p_s_sci1Msg)
{
	if(p_s_sci1Msg->CTimer)
	{
		p_s_sci1Msg->CTimer--;			  
	}
	else
	{
		p_s_sci1Msg->Error = TRUE;
		//data recover to inital value
		(void)memcpy(p_s_sci1Msg->Data,
					 p_s_sci1Msg->InitData,
					 p_s_sci1Msg->Length);
	}
}
/******************************************************************************/
/**
 * Function Name: app_sci1CheckTimeOutError
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				  draft 					yong.ding
 ******************************************************************************/
static void app_sci1CheckRxEventTimeOutError(UINT8 id,UINT8 sub_id)
{
	SCI_TX_MSG_S **ptr;
	ptr = p_s_sci1CycTxTable;
	while((*ptr)->Id != 0)
    {   
        if((*ptr)->SendType == SCI_EVENT &&((*ptr)->Id==id)&&((*ptr)->Sub_Id==sub_id))
        {
            (*ptr)->NobackTimer++;
			if((*ptr)->NobackTimer > (*ptr)->NobackTime)
			{
				(*ptr)->NobackTimer = 0;
				if((*ptr)->NobackRetry_cn < (*ptr)->NobackRetry_cn_max)
				{	
					(*ptr)->Trigger = TRUE;
				}
				if((*ptr)->NobackRetry_cn < (*ptr)->NobackRetry_cn_max)
				{
					(*ptr)->NobackRetry_cn++;
				}
			}
			break;
        }
        ptr++;
    }

}
/******************************************************************************/
/**
 * Function Name: app_sci1CheckTimeOutError
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				  draft 					yong.ding
 ******************************************************************************/

static void app_sci1CheckRxLongEventTimeOutError(UINT8 id,UINT8 sub_id)
{
	SCI_TX_LONG_MSG_S **ptr;
	ptr = p_s_sci1LongCycTxTable;
	while((*ptr)->Id != 0)
    {   
        if((*ptr)->SendType == SCI_EVENT &&((*ptr)->Id==id)&&((*ptr)->Sub_Id==sub_id))
        {
            (*ptr)->NobackTimer++;
			if((*ptr)->NobackTimer > (*ptr)->NobackTime)
			{
				(*ptr)->NobackTimer = 0;
				if((*ptr)->NobackRetry_cn < (*ptr)->NobackRetry_cn_max)
				{	
					(*ptr)->Trigger = TRUE;
				}
				if((*ptr)->NobackRetry_cn < (*ptr)->NobackRetry_cn_max)
				{
					(*ptr)->NobackRetry_cn++;
				}
			}
			break;
        }
        ptr++;
    }
}
/******************************************************************************/
/**
 * Function Name: app_sci1CheckTimeout
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				        					yong.ding
 ******************************************************************************/

static void app_sci1WriteWFeedbackFlag(UINT8 id,UINT8 sub_id,BOOL set)
{
	SCI_RX_MSG_S **ptr;
	SCI_RX_LONG_MSG_S **ptr2;
	ptr = p_s_sci1RxTable;
	while((*ptr)->Id != 0)
    {   
        if((*ptr)->SendType == SCI_EVENT &&((*ptr)->Id==id)&&((*ptr)->Sub_Id==sub_id))
        {
            (*ptr)->Waite_Feedback_Flag = set;
			return ;
        }
        ptr++;
    }

	ptr2 = p_s_sci1LongRxTable;
	while((*ptr2)->Id != 0)
    {   
        if((*ptr2)->SendType == SCI_EVENT &&((*ptr2)->Id==id)&&((*ptr2)->Sub_Id==sub_id))
        {
            (*ptr2)->Waite_Feedback_Flag = set;
			return ;
        }
        ptr2++;
    }
}

/******************************************************************************/
/**
 * Function Name: app_sci1CheckTimeout
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				        					yong.ding
 ******************************************************************************/

BOOL app_sci1GetError(SCI_RX_MSG_S s_sci1Msg)
{
      return s_sci1Msg.Error;
}

/******************************************************************************/
/**
 * Function Name: app_sci1CheckTimeout
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				        					yong.ding
 ******************************************************************************/

static void app_sci1ClearError(SCI_RX_MSG_S* p_s_sci1Msg)
{
    p_s_sci1Msg->Error = FALSE;
}

/******************************************************************************/
/**
 * Function Name: app_sci1CheckTimeout
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				        					yong.ding
 ******************************************************************************/

void app_sci1ClearAllError(void)
{
    SCI_RX_MSG_S **ptr;
    ptr = p_s_sci1RxTable;
    while((*ptr)->Id != 0)
    {
        app_sci1ClearError(*ptr);
        ptr++;
    }
}


/*interface*/
void app_sci1TxCtrl(BOOL CMD)
{
    if(CMD)
    {
        b_sci1EnableTx = TRUE;
    }
    else
    {
        b_sci1EnableTx = FALSE;
    }    
}

BOOL app_sci1GetTxCtrl( void )
{
    return b_sci1EnableTx;
}

void app_sci1RxCtrl(BOOL CMD)
{
    if(CMD)
    {
        b_sci1EnableRx = TRUE;
    }
    else
    {
        b_sci1EnableRx = FALSE;
    }
    
}

static void app_sci1MoniterErr(void)
{

}





/*****************************************************************************
** End File
*****************************************************************************/

